<?php
class Zendvn_Model_Table_User extends Zend_Db_Table{
	protected $_name = "users";
	protected $_primary = "id"; 
	protected $_listSQL;
	
	public function searchFields($arrParam = null,$options = null){
		$translate = new Zend_View_Helper_Translate();
		return array(
					'u.email'			=>$translate->translate('- Email field'),
					'u.id'				=>$translate->translate('- ID field'),
					'u.first_name'		=>$translate->translate('- First name field'),
					'u.last_name'		=>$translate->translate('- Last name field'),
					'u.birthday'		=>$translate->translate('- Birthday field'),
					'u.created'			=>$translate->translate('- Created field'),
					'u.created_ip'		=>$translate->translate('- Created ip field'),
					'u.visited'			=>$translate->translate('- Visited field'),
					'u.visited_ip'		=>$translate->translate('- Visited ip field'),
					'u.sign'			=>$translate->translate('- Sign field'),
					);
	}
	
	public function sortItem($arrParam = null,$options = null){
		if($options == null){
			
			foreach ($arrParam['cid'] as $val){				
				$where = 'id = ' . $val;
				$data = array('ordering'=>$arrParam['ordering'][$val]);
				$this->update($data, $where);
				
			}
		}
	}
	
	public function defaultGroup($arrParam = null,$options = null){
		if($options  == null){
			
			$default = ($arrParam['s'] == 0 )? 1:0;	
			$itemInfo = $this->getItem(array('id'=>(int)$arrParam['id']));
			
			if($default == 1){				
				$data = array('group_default' =>$default,);
				$where = "id = " . (int)$arrParam['id'];
				$this->update($data, $where);
			}else{
				if($arrParam['id'] > 5){
					$data = array('group_default' =>$default,);
					$where = "id = " . (int)$arrParam['id'];
					$this->update($data, $where);
				}
			}
		}
	}
	
	public function uploadImages($arrParam = null,$options = null){
		
		$data = array('avatar' => $arrParam['picture']);
		$where = 'id = ' . (int)$arrParam['id'];
		return $this->update($data, $where);
	}
	
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';	
			
			$itemInfo = $this->getItem(array('id'=>(int)$arrParam['id']));
		
			if($itemInfo['group_default'] != 1){											
				$data = array('status'=>$status);
				$where = 'id = ' . (int)$arrParam['id'];
				$this->update($data, $where);
			}
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){
						$itemInfo = $this->getItem(array('id'=>(int)$val));
						
						if($itemInfo['group_default'] !=1 ){
							$where = 'id = ' . $val;
							$data = array('status'=>$status);
							$this->update($data, $where);
						}
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->user($arrParam,$options,$this);
	}
	
	public function getItem($arrParam = null,$options = null){
		$db = $this->getAdapter();
		
		if($options == null){
			$where = 'id = ' . (int)$arrParam['id'];
			$res = $this->fetchRow($where);
			if(is_object($res)){
				$result = $res->toArray();
			}
		}
		
		if($options['task'] == 'check-email'){
			$where = 'email = "' . $arrParam['email'] . '"';
			$result = $this->fetchRow($where);
		}
		
		if($options['task'] == 'info'){
			$select = $db->select()
						->from($this->_name . ' AS u')
						->joinLeft('user_and_group AS uag', 'u.id = uag.user_id',array())
					 	->joinLeft('user_group AS g', 'g.id = uag.user_group_id', array('g.name as group_name'))
						->where('u.id = ?',$arrParam['id'],1);
			
			$result = $db->fetchRow($select);
			
		}
		return $result;
	}
	
	public function listItem($arrParam = null,$options = null){
		
		$db = $this->getAdapter();
		$select = $db->select()
					 ->from($this->_name . ' AS u')
					 ->joinLeft('user_and_group AS uag', 'u.id = uag.user_id',array())
					 ->joinLeft('user_group AS g', 'g.id = uag.user_group_id', array('g.name as group_name'))
					 ->where('uag.group_default = 1');
		$arrFilter = $arrParam['arrFilter'];
		
		if(!empty($arrFilter['col'])){
			$select->order($arrFilter['col'] . ' ' . $arrFilter['order']);			
		}
		
		if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
			$select->where('u.status = ?',$arrFilter['status']);
		}
		
		if(!empty($arrFilter['group_id']) && $arrFilter['group_id'] != 0){
			$select->where('g.id = ?', $arrFilter['group_id']);
		}
		
		if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
			$keyword = '%' . $arrFilter['keyword'] . '%';
			$select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
		}
		
		$this->_listSQL = $select;
		
		$panigator = $arrParam['panigator'];	
		$select->limitPage($panigator['page'], $panigator['rowCount']);
		
		$result = $db->fetchAll($select);		 
		
		return $result;
	}
	
	public function totalItem($arrParam = null,$options = null){
		
		$db = $this->getAdapter();
		$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
		$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
		$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
		$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
		$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
		$select->columns('COUNT(u.id) AS total');	
		$result = $db->fetchOne($select);
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		return $this->$function($arrParam,$options);
	}
	
	public function register($arrParam = null){
			
		$row = $this->fetchNew();
		$row->username		= $arrParam['email'];			
		$row->name			= $arrParam['email'];			
		$row->password		= Zendvn_Encode_Password::encode($arrParam['password']);
		$row->email			= $arrParam['email'];
		$row->firstname		= $arrParam['firstname'];
		$row->lastname		= $arrParam['lastname'];
			
		$row->status		= 'inactive';
		$row->register_date	= date('Y-m-d H:i:s');
		$row->created		= date('Y-m-d');
		$row->register_ip	= $_SERVER['REMOTE_ADDR'];
		$row->first_login	= 0;
		$row->update_info	= 0;
			
		$result = $row->save();
		$db = $this->getAdapter();
		
		$bind = array('user_id'=>$result,'user_group_id'=> 4,'group_default'=>1);
		$db->insert('user_and_group', $bind);
		
		return $result;
	}
	
	public function adminAdd($arrParam = null){
		
		$row = $this->fetchNew();
		$row->avatar		= $arrParam['avatar'];			
		$row->password		= Zendvn_Encode_Password::encode($arrParam['password']);
		$row->email			= $arrParam['email'];
		$row->first_name	= $arrParam['first_name'];
		$row->last_name		= $arrParam['last_name'];
		$row->birthday		= $arrParam['birthday'];
		$row->sign			= $arrParam['sign'];
		
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		$row->created		= date('Y-m-d H:i:s');
		$row->created_ip 	= $_SERVER['REMOTE_ADDR'];
			
		$row->save();
			
		$userID = $row->id;
			
		$db = $this->getAdapter();
		
		$bind = array('user_id'=>$userID,'user_group_id'=>$arrParam['group_id'],'group_default'=>1);
		$db->insert('user_and_group', $bind);
		
		$ext_group = $arrParam['ext_group'];
		if(count($ext_group)>0){
			foreach ($ext_group as $key => $val){					
				if($val!= 0 && $val!= 5 && $val != $arrParam['group_id']){
					$extArray[] = $val;
				}
			}
			if(count($extArray)>0){
				foreach ($extArray as $key => $val){	
					$bind = array('user_id'=>$userID,
								  'user_group_id'=>$val,
								  'group_default'=>0);
					$db->insert('user_and_group', $bind);
				}
			}
		}
	}
	
	public function adminEdit($arrParam = null){
		
		$where  = 'id = ' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->avatar		= $arrParam['avatar'];		
		if(trim($arrParam['password'])!= ''){	
			$row->password		= Zendvn_Encode_Password::encode($arrParam['password']);
		}
		$row->email			= $arrParam['email'];
		$row->first_name	= $arrParam['first_name'];
		$row->last_name		= $arrParam['last_name'];
		$row->birthday		= $arrParam['birthday'];
		$row->sign			= $arrParam['sign'];
			
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		
		$row->save();
	
		$userID = $row->id;
			
		$db = $this->getAdapter();
		$where = 'user_id = ' . $userID;
		$db->delete('user_and_group',$where);
		$bind = array('user_id'=>$userID,'user_group_id'=>$arrParam['group_id'],'group_default'=>1);
		$db->insert('user_and_group', $bind);
			
		$ext_group = $arrParam['ext_group'];
		if(count($ext_group)>0){
			foreach ($ext_group as $key => $val){					
				if($val!= 0 && $val != $arrParam['group_id']){
					$extArray[] = $val;
				}
			}
			
			if(count($extArray)>0){
				foreach ($extArray as $key => $val){	
					$bind = array('user_id'=>$userID,
								  'user_group_id'=>$val,
								  'group_default'=>0);
					$db->insert('user_and_group', $bind);
				}
			}
		}
	}
	
	public function getConfirmCode($arrParam,$options=null){
		$auto_code = new Zendvn_RandomString();
		$confirm_code = $auto_code->createRandomString(11);
		
		$encode = new Zendvn_Encode_Password();
		$pass = $encode->encode($confirm_code);
		
		if($options['task'] == 'user-active'){
			$code = 'active_code';
		}else{
			$code = 'confirm_code';
		}
		$result = $this->fetchRow('email="'.$arrParam['email'].'"');
		
		$result->$code = $pass;
		$result->save();
		
		if(is_object($result)){
			$result = $result->toArray();
		}
		
		$result[$code] = $confirm_code;
		
		return $result;
	}
	
	public function updatePass($arrParam, $options = null){
		if(empty($arrParam['id'])) return false;
		if($options == null){
			$result = $this->update(
				array(
					'password'=>$arrParam['confirm_code'],
					'confirm_code'=>'',
					'status'=>'active'
				),
				'id = '.$arrParam['id']
			);
		}
		
		if($options['task'] == 'user-active'){
			$result = $this->update(
				array(
					'status' => 'active',
					'active_code'=>''
				),
				'id = '.$arrParam['id']
			);
		}
		return $result;
	}
	
	public function run($arrParam,$options){
		$function  = Zendvn_System_Function::call($options['run']);
		return $this->$function($arrParam,$options);
	}
}
























